﻿

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using GPCSC.Web;
using GPCSC.BLL;
using NickLee.Web.UI;
using NickLee.Common;
using GPCSC.Model;

namespace GPCSC.Web.SYS_ORG
{
    public partial class ATIONList :  PageBase
    {
        SqlReadXml org = new SqlReadXml();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region TreeView结构无线级填充

        protected bool buildTree(NickLee.Web.UI.TreeView TreeViewName, DataSet ds)
        {
            //清空所有节点
            TreeViewName.Nodes.Clear();

            //如果为最高级，则修改以适应ds的relation中的关系
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (dbRow["CL_PARMENUID_PK"] != null && Usual.f_convertGuid(dbRow["CL_PARMENUID_PK"]) == Usual.f_convertGuid("00000000-0000-0000-0000-000000000000"))
                {
                    dbRow["CL_PARMENUID_PK"] = DBNull.Value;
                }
            }
            //设置ds中父子级relation
            try
            {
                ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["CL_MENU_SOID"], ds.Tables[0].Columns["CL_PARMENUID_PK"]);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

            //循环绑定父node
            foreach (DataRow dbRow in ds.Tables[0].Rows)
            {
                if (dbRow.IsNull("CL_PARMENUID_PK"))
                {

                    TreeViewNode newNode =
                        CreateTreeNode(dbRow["MENUName"].ToString(), dbRow["CL_MENU_SOID"].ToString(),
                                       "",
                                       dbRow["REMARK"].ToString(),
                                       "");
                    TreeViewName.Nodes.Add(newNode);
                    buildTrees(newNode, dbRow["CL_MENU_SOID"].ToString());

                    PopulateSubTree(dbRow, newNode, TreeViewName);
                }
            }
            return true;
        }

        /// <summary>
        /// 循环绑定子node
        /// </summary>
        /// <param name="dbRow">dataset的行</param>
        /// <param name="node">需要添加的节点</param>
        private void PopulateSubTree(DataRow dbRow, TreeViewNode node, NickLee.Web.UI.TreeView TreeViewName)
        {
            foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
            {
                //TreeViewNode childNode =
                //    CreateTreeNode(childRow["MENUName"].ToString(), childRow["CL_MENU_SOID"].ToString(), childRow["VALUEURL"].ToString(),
                //                   childRow["TOOLTIP"].ToString(),
                //                   childRow["TARGET"].ToString(),
                //                   childRow["VALUEURL_2"].ToString());
                TreeViewNode childNode =
                    CreateTreeNode(childRow["MENUName"].ToString(), childRow["CL_MENU_SOID"].ToString(),
                                   "",
                                   childRow["REMARK"].ToString(),
                                   "");
                node.Nodes.Add(childNode);
                buildTrees(childNode, childRow["CL_MENU_SOID"].ToString());
                //Hashtable table = new Hashtable();
                //table.Add("@MENUID", childNode.ID);
                //DataTable dt = org.ReaderDataSet("sel_value_menu", table).Tables[0];
                // f_treeViewValueDataBind(TreeViewName, childNode, dt);

                PopulateSubTree(childRow, childNode, TreeViewName);
            }
        }

        /// <summary>
        /// 给所有node附加属性
        /// </summary>
        /// <param name="text">呈现文本</param>
        /// <param name="ID">主键</param>
        /// <param name="expand">是否扩展</param>
        /// <param name="Value">后台值</param>
        /// <param name="toolTip">鼠标移动上去的文本提示</param>
        /// <param name="target">Target</param>
        /// <param name="cutCssClass">Value2</param>
        /// <returns>返回node</returns>
        private TreeViewNode CreateTreeNode(string text, string ID, string Value, string toolTip, string target)
        {
            TreeViewNode node = new TreeViewNode();
            if (text.Trim() == "分解目标")
            {
                int i = 0;
            }
            node.Text = text;
            node.ID = "P_" + ID;
            node.Value = Value;
            node.ToolTip = toolTip;


            node.Target = target;


            node.ShowCheckBox = true;
            //if (node.Target == "N_SEC_VALUE")
            //{
            //    node.ShowCheckBox = true;
            //}
            node.Expanded = true;
            return node;

        }

        /// <summary>
        /// 给所有node附加属性
        /// </summary>
        /// <param name="text">呈现文本</param>
        /// <param name="ID">主键</param>
        /// <param name="expand">是否扩展</param>
        /// <param name="Value">后台值</param>
        /// <param name="toolTip">鼠标移动上去的文本提示</param>
        /// <param name="target">Target</param>
        /// <param name="cutCssClass">Value2</param>
        /// <returns>返回node</returns>
        private TreeViewNode CreateTreeNodes(string text, string ID, string Value, string toolTip, string target, string cutCssClass)
        {
            TreeViewNode node = new TreeViewNode();
            node.Text = text;
            node.ID = "P_" + ID;
            node.Value = Value;
            node.ToolTip = toolTip;
            node.Target = target;
            node.CutCssClass = cutCssClass;


            node.ShowCheckBox = true;
            //if (node.Target == "N_SEC_VALUE")
            //{
            //    node.ShowCheckBox = true;
            //}

            node.Visible = true;
            node.Expanded = true;
            return node;

        }


        #endregion


        #region TreeView结构行为

        protected void buildTrees(NickLee.Web.UI.TreeViewNode TreeViewName, string Meunid)
        {
            
            //DataSet ds = new BLL.CL_VALUEMENU_S().GetList("CL_MENUID_PK='" + Meunid + "'");


            ////循环绑定父node
            //foreach (DataRow dbRow in ds.Tables[0].Rows)
            //{

            //    TreeViewNode newNode =
            //        CreateTreeNodeV(dbRow["ControlNAME"].ToString(), dbRow["CL_VALUEMENU_SOID"].ToString(), "",
            //                       "", "",
            //                       "");
            //    TreeViewName.Nodes.Add(newNode);
            //}

        }



        /// <summary>
        /// 给所有node附加属性
        /// </summary>
        /// <param name="text">呈现文本</param>
        /// <param name="ID">主键</param>
        /// <param name="expand">是否扩展</param>
        /// <param name="Value">后台值</param>
        /// <param name="toolTip">鼠标移动上去的文本提示</param>
        /// <param name="target">Target</param>
        /// <param name="cutCssClass">Value2</param>
        /// <returns>返回node</returns>
        private TreeViewNode CreateTreeNodeV(string text, string ID, string Value, string toolTip, string target, string cutCssClass)
        {
            TreeViewNode node = new TreeViewNode();
            node.Text = text;
            node.ID = "V_" + ID;
            node.Value = Value;
            node.ToolTip = toolTip;
            node.Target = target;
            node.CutCssClass = cutCssClass;


            node.ShowCheckBox = true;
            //if (node.Target == "N_SEC_VALUE")
            //{
            //    node.ShowCheckBox = true;
            //}

            node.Visible = true;
            node.Expanded = true;
            return node;

        }


        #endregion
        #region 一级函数



        public void f_configentityTree()
        {
            var ztype = Usual.f_querystringToString("Type");
            Guid ID = Usual.f_convertGuid(Usual.f_querystringToString("ID"));
            string name = Usual.f_querystringToString("Text");
            this.Type_Name.Text = ztype.ToString() + "：" + name;
            var ds = org.ReaderDataSet("sel_menu_tree");
            this.buildTree(this.entityTree, ds);
            var table = new Hashtable();

            DataTable dTable = new DataTable();

            if (ztype == "USER")//如果为用户
            {
                table.Add("@USERID", ID);
                lblMessage.Text = "当前权限配置为用户";
                // dTable = org.ReaderDataSet("sel_usermenu_part", table).Tables[0];

                //读取用户菜单表中对应的菜单
                dTable = org.ReaderDataSet("sel_usermenu_all2", table).Tables[0];

            }
            else if (ztype == "ROLE")//为角色
            {
                table.Add("@ROLEID", ID);
                lblMessage.Text = "当前权限配置为角色";
                //dTable = org.ReaderDataSet("sel_AUTHORIZATION_ROLE", table).Tables[0];
                dTable = org.ReaderDataSet("sel_rolemenu_part", table).Tables[0];
            }
            else if (ztype == "POST")//为部门
            {
                table.Add("@POSTID", ID);
                lblMessage.Text = "当前权限配置为部门";
                //dTable = org.ReaderDataSet("sel_AUTHORIZATION_ROLE", table).Tables[0];
                dTable = org.ReaderDataSet("sel_postmenu_part", table).Tables[0];
            }
            else if (ztype == "Depart")//为岗位
            {
                table.Add("@DepartID", ID);
                lblMessage.Text = "当前权限配置为岗位";
                //dTable = org.ReaderDataSet("sel_AUTHORIZATION_POST", table).Tables[0];
                dTable = org.ReaderDataSet("sel_departmenu_part", table).Tables[0];
            }

            for (int i = 0; i < dTable.Rows.Count; i++)
            {
                var control = this.entityTree.FindNodeById("P_" + dTable.Rows[i]["CL_MENUID_PK"].ToString());
                if (control != null)
                {
                    control.Checked = true;
                }
                var controls = this.entityTree.FindNodeById("V_" + dTable.Rows[i]["CL_VALUE_PK"].ToString());
                if (controls != null)
                {
                    controls.Checked = true;
                }
            }
        }

        #endregion

        #region 服务器按钮事件

        protected void CONFIG_Click(object sender, EventArgs e)
        {
            var usernodes = this.entityTree.CheckedNodes.ToList<TreeViewNode>();
            var ztype = Usual.f_querystringToUpperString("Type");
            var zid = Usual.f_querystringToUpperString("ID");
            //TreeViewNode[] systemnodes = this.zyTreeView.CheckedNodes;

            try
            {
                BLL.CL_UMDRP_M authbll = new GPCSC.BLL.CL_UMDRP_M();
                Model.CL_UMDRP_M auth = new GPCSC.Model.CL_UMDRP_M();
                DataSet ds = new DataSet();

                //byte[] userInfo = (byte[])base.o_baseTable[c_obj.s_userInfo];
                //Hashtable userInfoTable = CompressData.UnCompressHashtable(userInfo);
                //Model.CL_USER_S Userinfo = (Model.CL_USER_S)userInfoTable["SYS_USER"];
                //Model.CL_USER_S User = new Model.CL_USER_S();
                //User = Userinfo;


                //if (ztype == "USER")
                //{
                //    base.o_uiTable.Add("@USERID", Usual.f_convertGuid(zid));
                //    //先查询有无记录，有则编辑；无则创建
                //    ds = org.ReaderDataSet("sel_UMDRP_USER", base.o_uiTable);
                //    if (ds.Tables[0].Rows.Count > 0)
                //    {
                //        auth.EDITERID = User.CL_USER_SOID;
                //        auth.EDITERNAME = User.REALNAME;
                //        auth.EDITDATE = System.DateTime.Now;
                //    }
                //    else
                //    {
                //        auth.CREATEDATE = System.DateTime.Now;
                //        auth.CREATERID = User.CL_USER_SOID;
                //        auth.CREATERNAME = User.REALNAME;

                //    }
                //    org.NotDoSQL("del_UMDRP_USER", base.o_uiTable);
                ////}
                //else if (ztype == "ROLE")
                //{
                   base.o_uiTable.Add("@ROLEID", Usual.f_convertGuid(zid));
                    ////先查询有无记录，有则编辑；无则创建
                    //ds = org.ReaderDataSet("sel_UMDRP_ROLE", base.o_uiTable);
                    //if (ds.Tables[0].Rows.Count > 0)
                    //{
                    //    auth.EDITERID = User.CL_USER_SOID;
                    //    auth.EDITERNAME = User.REALNAME;
                    //    auth.EDITDATE = System.DateTime.Now;
                    //}
                    //else
                    //{
                    //    auth.CREATEDATE = System.DateTime.Now;
                    //    auth.CREATERID = User.CL_USER_SOID;
                    //    auth.CREATERNAME = User.REALNAME;

                    //}
                    org.NotDoSQL("del_UMDRP_ROLE", base.o_uiTable);
                //}
                //else if (ztype == "POST")
                //{
                //    base.o_uiTable.Add("@POSTID", Usual.f_convertGuid(zid));
                //    //先查询有无记录，有则编辑；无则创建
                //    ds = org.ReaderDataSet("sel_UMDRP_POST", base.o_uiTable);
                //    if (ds.Tables[0].Rows.Count > 0)
                //    {
                //        auth.EDITERID = User.CL_USER_SOID;
                //        auth.EDITERNAME = User.REALNAME;
                //        auth.EDITDATE = System.DateTime.Now;
                //    }
                //    else
                //    {
                //        auth.CREATEDATE = System.DateTime.Now;
                //        auth.CREATERID = User.CL_USER_SOID;
                //        auth.CREATERNAME = User.REALNAME;

                //    }
                //    org.NotDoSQL("del_UMDRP_POST", base.o_uiTable);
                //}
                //else if (ztype == "DEPART")
                //{
                //    base.o_uiTable.Add("@DepartID", Usual.f_convertGuid(zid));
                //    //先查询有无记录，有则编辑；无则创建
                //    ds = org.ReaderDataSet("sel_UMDRP_Depart", base.o_uiTable);
                //    if (ds.Tables[0].Rows.Count > 0)
                //    {
                //        auth.EDITERID = User.CL_USER_SOID;
                //        auth.EDITERNAME = User.REALNAME;
                //        auth.EDITDATE = System.DateTime.Now;
                //    }
                //    else
                //    {
                //        auth.CREATEDATE = System.DateTime.Now;
                //        auth.CREATERID = User.CL_USER_SOID;
                //        auth.CREATERNAME = User.REALNAME;

                //    }
                //    org.NotDoSQL("del_UMDRP_Depart", base.o_uiTable);
                //}

                //else
                //{
                //    JSUtil.f_ajaxAlert(this, "请先选择部门、岗位、用户或者系统角色！");

                //    return;
                //}
                //删除操作

                Guid MENUID = new Guid();
                usernodes.ForEach(obj =>
                {
                    string arry = obj.ID.ToString();
                    string[] temp = arry.Split('_');
                    if (temp[0] == "P")
                    {
                        auth.CL_MENUID_PK = Usual.f_convertGuid(temp[1]);

                    }
                    else if (temp[0].ToString() == "V")
                    {
                        auth.CL_VALUE_PK = Usual.f_convertGuid(temp[1]);
                    }
                    Guid authid = Guid.NewGuid();
                    auth.CL_UMDRP_MOID = authid;
                    if (ztype == "USER")
                    {
                        auth.CL_USERID_PK = Usual.f_convertGuid(zid);
                    }
                    else if (ztype == "ROLE")
                    {
                        auth.CL_ROLEID_PK = Usual.f_convertGuid(zid);
                    }
                    else if (ztype == "POST")
                    {
                        auth.CL_POSTID_PK = Usual.f_convertGuid(zid);
                    }
                    else if (ztype == "DEPART")
                    {
                        auth.CL_DepartID_PK = Usual.f_convertGuid(zid);
                    }
                    authbll.Add(auth);

                });
                JSUtil.f_ajaxAlert(this, "该用户权限配置完成！");


            }
            catch (System.Exception ee)
            {
                //m_loger.Error("用户:" + mapUser.m_strUserName + "为用户:" + base.o_uiTable["GWM_UID"] + "权限配置完成！" + ee.Message);
                JSUtil.f_ajaxAlert(this, ee.Message);

            }
        }

        protected void Callback2_Callback(object sender, CallBackEventArgs e)
        {
            f_configentityTree();
            this.entityTree.RenderControl(e.Output);
            this.Type_Name.Visible = true;
        }

        #endregion
    }
}
